package com.example.handpose_front.HomePage;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.handpose_front.MainPage.MainActivity;
import com.example.handpose_front.R;
import com.example.handpose_front.callback.HomeCallBack.IExchangeLoginMode;
import com.example.handpose_front.dto.DataResponse;

public class VerifyCodeLoginFragment extends Fragment {
    private View rootView;
    // verifycodelogin输入框
    private EditText phoneEt;
    private EditText verifyCodeEt;
//    private VerifyCodeButton verifyCodeButton;
    private TextView exchangeTV;
    private Button loginBtn;

    // 用户输入
    private String phoneStr;
    private String verifyCodeStr;

    // 响应体
    private DataResponse dataResponse;

    private IExchangeLoginMode exchangeLoginMode;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 确保只赋值一次
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_verify_code_login, container, false);
        }

        // 初始化控件
        initView();
        // 为控件添加事件(交换事件，登录事件)
        initEvent();

        return rootView;
    }

    // 用来接收父类实现的接口（callBack）
    public void setExchangeLoginMode(IExchangeLoginMode exchangeLoginMode) {
        this.exchangeLoginMode = exchangeLoginMode;
    }

    private void initEvent() {
        // 交换登录模式事件
        exchangeTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 进行登录模式的切换
                exchangeLoginMode.exchangeLoginMode();
            }
        });
        // 点击登录按钮
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 验证码登录
                if (v.getId() == loginBtn.getId()){
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(getActivity(), "登录成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        // 初始化基本控件
        phoneEt = rootView.findViewById(R.id.verify_login_et_phone);
        verifyCodeEt = rootView.findViewById(R.id.login_et_verifyCode);
//        verifyCodeButton = rootView.findViewById(R.id.get_login_verification_code);
        loginBtn = rootView.findViewById(R.id.verify_login_btn);
        exchangeTV = rootView.findViewById(R.id.login_tv_exchange);
    }

}